package com.zbkj.crmeb.front.request;

import com.exception.ApiException;
import com.utils.MyAssert;
import com.zbkj.crmeb.Contract;
import com.zbkj.crmeb.Extract;
import com.zbkj.crmeb.version.model.VersionRaw;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

public class UpdateDto {
    @Data
    @ApiModel("UpdateReq")
    public static class UpdateReq implements Extract<VersionRaw> {
        @ApiModelProperty(value = "app标识", example = "144asdf44asdf", required = true)
        String appid;
        @ApiModelProperty(value = "客户端版本号", example = "1.0.0", required = true)
        String version;
        @ApiModelProperty(value = "客户端imei", example = "xdfasdfasdf", required = true)
        String imei;

        @Override
        public VersionRaw extractFrom(VersionRaw obj) {
            String[] versions = version.split("\\.");
            MyAssert.assertTrue(versions.length == 3, new ApiException("版本号错误"));
            obj.setMainVersion(Integer.parseInt(versions[0]));
            obj.setMiniVersion(Integer.parseInt(versions[1]));
            obj.setResourceVersion(Integer.parseInt(versions[2]));
            return obj;
        }

        @Override
        public String toString() {
            return "UpdateReq{" +
                    "appid='" + appid + '\'' +
                    ", version='" + version + '\'' +
                    ", imei='" + imei + '\'' +
                    '}';
        }
    }

    @Data
    @ApiModel("UpdateResp")
    public static class UpdateResp implements Contract<VersionRaw> {
        @ApiModelProperty(value = "版本", example = "complete", required = true)
        String version;
        @ApiModelProperty(value = "是否跟新", required = true)
        Boolean isUpdate;
        @ApiModelProperty(value = "androidUrl", required = true)
        String androidUrl;
        @ApiModelProperty(value = "iosUrl", required = true)
        String iosUrl;
        @ApiModelProperty(value = "是否强制安装", required = true)
        Boolean isForce;
        @ApiModelProperty(value = "描述", required = true)
        String description;

        @Override
        public UpdateResp applyTo(VersionRaw obj) {
            this.version = obj.getMainVersion() + "." + obj.getMiniVersion() + "." + obj.getResourceVersion();
            this.isForce = obj.getIsForce();
            this.description = obj.getDescription();
            return this;
        }
    }
}
